import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
import 'package:top_foundation/component/page_state/widgets/empty.dart';
import 'package:whale/helper/component/component.dart';
import 'package:whale/helper/utils/util.dart';
import 'package:whale/pages/means/main/widgets/select.dart';

import 'index.dart';
import 'widgets/widgets.dart';

class MeansPage extends StatelessWidget {
  MeansPage({Key? key}) : super(key: key);

  final controller = Get.put(MeansController());
  final state = Get.find<MeansController>().state;
  // 主视图
  Widget _buildView() {
    var list = state.data[state.reportType];
    return Column(
      children: [
        BrnTabBar(
          tabs: state.titles.map((e) => BadgeTab(text: e)).toList(),
          controller: state.tabCtrl,
          tabHeight: 44.r,
          labelStyle: TextStyle(
            fontSize: 14.sp,
          ),
        ),
        MeansSelectView(
          titles: state.selectionTitles,
          complete: controller.selectionResults,
        ),
        Expanded(
          child: SmartRefresher(
            controller: state.rc,
            enableTwoLevel: true,
            enablePullDown: true,
            enablePullUp: true,
            onRefresh: controller.onRefresh,
            onLoading: controller.onLoading,
            child: isZeroList(list)
                ? noDataView()
                : ListView.separated(
                    itemBuilder: (_, idx) {
                      var md = list![idx];
                      return MeansCell(
                        md: md,
                      );
                    },
                    separatorBuilder: (_, idx) {
                      return Container(
                        height: 1,
                        color: ColorsUtil.bg,
                      );
                    },
                    itemCount: list?.length ?? 0),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MeansController>(
      builder: (_) {
        return Scaffold(
          appBar: AppBar(
            leading: const SizedBox(),
            leadingWidth: 0,
            elevation: 0,
            backgroundColor: Colors.white,
            title: SizedBox(
              height: 40.r,
              child: Row(
                children: [
                  Expanded(
                      child: TextField(
                    controller: state.inputCtrl,
                    textAlignVertical: TextAlignVertical.center,
                    textInputAction: TextInputAction.search,
                    decoration: InputDecoration(
                      hintText: '请输入关键字',
                      hintStyle: TextStyle(
                        color: ColorsUtil.placeholder,
                        fontSize: 14.sp,
                      ),
                      prefixIcon: Icon(
                        IconUtil.search,
                        size: 20.r,
                        color: ColorsUtil.title,
                      ),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(3.r),
                        borderSide: BorderSide.none,
                      ),
                      filled: true,
                      fillColor: ColorsUtil.bg,
                      isCollapsed: true,
                    ),
                    onSubmitted: (str) => controller.actionSearch(),
                  )),
                  SizedBox(
                    width: 10.r,
                  ),
                  GestureDetector(
                    onTap: () => controller.actionSearch(),
                    child: Text(
                      '搜索',
                      style: TextStyle(color: Colors.black, fontSize: 14.r),
                    ),
                  )
                ],
              ),
            ),
          ),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
